//
//  FZOAuthViewController.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/20/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

class FZOAuthViewController: UIViewController {
    
    //MARK:==========================初始化界面==========================
    
    override func loadView() {
        view = webView
        let url = NSURL(string: AUTHORIZATION)
        
        if let u =  url {
            let request: NSURLRequest = NSURLRequest(URL: u)
            webView.loadRequest(request)
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setupUI()
    }
    
    func setupUI() {
        
        view.backgroundColor = TheColor
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target: self, action: #selector(dismissClick))
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", target: self, action: #selector(autoFillClick))
        
        //MARK: 加载进度条
        navigationController?.view.insertSubview(progress, belowSubview: (navigationController?.navigationBar)!)
    }
    
    //MARK:==========================事件==========================
    
    func dismissClick(){
        self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
    }
    func autoFillClick(){
        
    }
    
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        progress.removeFu()
    }
    
    //MARK:==========================懒加载==========================
    
    lazy var webView: UIWebView = {
        
        let web: UIWebView = UIWebView()
        web.delegate = self;
        return web;
    }()
    //进度条
    lazy var progress: FZOAuthProgressView = FZOAuthProgressView()
    
}

//MARK:==========================分类==========================
extension FZOAuthViewController: UIWebViewDelegate{
    //开始加载
    func webViewDidStartLoad(webView: UIWebView) {
        progress.startAnim()
    }
    //加载结束会调
    func webViewDidFinishLoad(webView: UIWebView) {
        progress.stopAnim()
    }
    //加载出错会调用
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        
    }
    //开始加载网页会调用
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        //获取请求地址，如果没有，就返回失败
        guard let url = request.URL else{
            return false
        }
        //为什么加上这一层判断，
        //containsString 是否包含某个字符串
        if url.absoluteString.hasPrefix(APPREDIRECT_URI) {
            //
            if let q = url.query where q.containsString("code=") {
                
                let code = q.substringFromIndex("code=".endIndex)
                //开始根据code值请求access_token 值
                FZUserAccountViewModel.sharedUserAcount.loadUserAccount(code, isSuccess: { (isSucc) in
                    
                    if !isSucc{
                        return
                    }
                    
                    NSNotificationCenter.defaultCenter().postNotificationName(FZSWITCHROOTVIEWCONTROLLER, object: nil)
                })
                
            }
            return false
        }
        
        
        return true
    }
}
